package com.missilike.admin.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.missilike.biz.IMilAdvertService;
import com.missilike.core.result.CommonResult;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.request.MilAdvertForm;
import com.missilike.repository.entity.MilAdvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 广告 前端控制器
 * </p>
 *
 * @author kin
 * @since 2018-12-15
 */
@RestController
@RequestMapping("/api/mil/advert")
public class MilAdvertController {

    private final IMilAdvertService milAdvertService;

    @Autowired
    public MilAdvertController(IMilAdvertService milAdvertService) {
        this.milAdvertService = milAdvertService;
    }

    @RequestMapping(value = "page", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize) {
        IPage<MilAdvert> advertList = milAdvertService.getList(new Page<>(pageNum, pageSize));
        return ResultUtils.ok(advertList);
    }

    @RequestMapping(value = "info/{advertId}", method = RequestMethod.GET)
    public CommonResult info(@PathVariable("advertId") String advertId) {
        return ResultUtils.ok(milAdvertService.getById(advertId));
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public CommonResult save(@ModelAttribute MilAdvertForm milAdvertForm) {
        milAdvertService.upsert(milAdvertForm);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody Integer[] advertIds) {
        MilAdvert milAdvert = new MilAdvert();
        milAdvert.setStatus(1);
        milAdvertService.update(milAdvert, new UpdateWrapper<MilAdvert>()
                .lambda()
                .in(MilAdvert::getId, Arrays.asList(advertIds))
        );
        return ResultUtils.ok();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult update(@ModelAttribute MilAdvertForm milAdvertForm) {
        milAdvertService.upsert(milAdvertForm);
        return ResultUtils.ok();
    }
}

